<template>
  <div class="login">
    <div class="core">
      <h1 class="title">后台管理系统</h1>
      <el-tabs type="border-card" stretch v-model="currentTab">
        <el-tab-pane name="account">
          <template #label>
            <span><el-icon>
                <Monitor />
              </el-icon>账号登录</span>
          </template>
          <LoginAccount :account="account" ref="accountRef" />
        </el-tab-pane>
        <el-tab-pane name="register">
          <template #label>
            <span><el-icon>
                <Iphone />
              </el-icon>注册账号</span>
          </template>
          <LoginRegister ref="registerRef" @register-success="registerSuccess" />
        </el-tab-pane>
      </el-tabs>

      <div class="ctrl-area">
        <div :class="['account-control', { hide: currentTab !== 'account' }]">
          <el-checkbox v-model="isKeepPassword">记住密码</el-checkbox>
          <el-link type="primary">忘记密码</el-link>
        </div>
        <template v-if="currentTab === 'account'">
          <el-button type="primary" class="login-btn" @click="handleLoginClick">立即登录</el-button>
        </template>
        <template v-else>
          <el-button type="primary" class="login-btn" @click="registAccount">注册账号</el-button>
        </template>
      </div>

    </div>
  </div>
</template>

<script setup>
import LoginAccount from './components/login-account.vue'
import LoginRegister from './components/login-register.vue'
import { getCache } from '@/utils/auth.js'
import { ref, onMounted, reactive } from 'vue'

const isKeepPassword = ref(true)
const accountRef = ref(null)
const registerRef = ref(null)
const currentTab = ref('account')
const account = reactive({})

onMounted(() => {
  account.username = getCache('username') ?? ''
  account.password = getCache('password') ?? ''
})


const handleLoginClick = () => {
  if (currentTab.value === 'account') {
    accountRef.value?.loginAction(isKeepPassword.value)
  } else {
    console.log('phoneRef调用loginAction')
  }
}

function registAccount() {
  registerRef.value?.registCount()
}

// 利用子组件修改prop是对象类型时，是可以改变属性的，这个特性 打通组件内外数据
function registerSuccess(val) {
  currentTab.value = 'account'
  account.username = val.username
  account.password = val.password
}

</script>

<style scoped lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: url('../../assets/img/login-bg.svg');

  .core {
    width: 320px;
  }

  .title {
    text-align: center;
  }

  .account-control {
    margin-top: 10px;
    display: flex;
    justify-content: space-between;

    &.hide {
      visibility: hidden;
    }
  }

  .login-btn {
    width: 100%;
    margin-top: 10px;
  }
}
</style>
